package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constant.GlobalConstant;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dots.LikesBehaviorDto;
import com.heima.model.article.dots.ReadBehaviorDto;
import com.heima.model.article.dots.UnLikesBehaviorDto;
import com.heima.model.article.dots.UpdateArticleMsg;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.threadLocal.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BehaviorServiceImpl implements BehaviorService {
    @Autowired
    KafkaTemplate kafkaTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        if (dto==null||dto.getArticleId()==null||dto.getOperation()==null||dto.getType()==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        if (!LikesBehaviorDto.Operation.check(dto.getOperation().shortValue()))
        {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (!dto.getType().equals(LikesBehaviorDto.Type.ARTICLE.getValue()))
        {
            //TODO 动态，评论点赞
            throw new CustomException(AppHttpCodeEnum.FUNCTION_TO_BE_DEVELOPED);
        }
        UpdateArticleMsg updateArticleMsg = new UpdateArticleMsg();
        updateArticleMsg.setArticleId(dto.getArticleId());
        String key=GlobalConstant.ARTICLE_LIKE_PREFIX+dto.getArticleId();
        if (dto.getOperation().equals(LikesBehaviorDto.Operation.LIKE.getValue()))
        {
            if (stringRedisTemplate.opsForSet().add(key, AppThreadLocalUtil.getUserId().toString())!=0)
            {
//                kafkaTemplate.send(GlobalConstant.USER_LIKE,dto.getArticleId().toString());
                updateArticleMsg.setLikes(1);
            }
            else return ResponseResult.errorResult();
        }
        else{
            if (stringRedisTemplate.opsForSet().remove(key, AppThreadLocalUtil.getUserId().toString())!=0)
            {
//                kafkaTemplate.send(GlobalConstant.USER_CANCEL_LIKE,dto.getArticleId().toString());
                updateArticleMsg.setLikes(-1);
            }
            else return ResponseResult.errorResult();
        }
        kafkaTemplate.send(GlobalConstant.HOT_ARTICLE_SCORE_STREAM_TOPIC,JSON.toJSONString(updateArticleMsg));
        log.info("点赞已发送");
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        if (dto==null||dto.getArticleId()==null||dto.getCount()==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        String key=GlobalConstant.ARTICLE_READ_PREFIX+dto.getArticleId();
        stringRedisTemplate.opsForValue().increment(key,dto.getCount());
//        kafkaTemplate.send(GlobalConstant.USER_READ, JSON.toJSONString(dto));
        UpdateArticleMsg updateArticleMsg = new UpdateArticleMsg();
        updateArticleMsg.setArticleId(dto.getArticleId());
        updateArticleMsg.setViews(dto.getCount());
        kafkaTemplate.send(GlobalConstant.HOT_ARTICLE_SCORE_STREAM_TOPIC, JSON.toJSONString(updateArticleMsg));
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
        if (dto==null||dto.getArticleId()==null||dto.getType()==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        if(!UnLikesBehaviorDto.Type.check(dto.getType()))
        {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        String key=GlobalConstant.ARTICLE_NO_LIKE_PREFIX+dto.getArticleId();
//        UpdateArticleMsg updateArticleMsg = new UpdateArticleMsg();
//        updateArticleMsg.setArticleId(dto.getArticleId());
        if (dto.getType().equals(UnLikesBehaviorDto.Type.NO_LIKE.getValue()))
        {
            if (stringRedisTemplate.opsForSet().add(key, AppThreadLocalUtil.getUserId().toString())==0)
                return ResponseResult.errorResult();
        }
        else{
            if (stringRedisTemplate.opsForSet().remove(key, AppThreadLocalUtil.getUserId().toString())==0)
                return ResponseResult.errorResult();
        }
        return ResponseResult.okResult();
    }
}
